package com.qf.dao.impl;


import com.qf.dao.BooksBorrowDao;
import com.qf.entity.Books;
import com.qf.entity.BooksBorrow;
import com.qf.entity.BorrowCard;
import com.qf.entity.Rules;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class BooksBorrowDaoImpl implements BooksBorrowDao {
    @Override
    public BorrowCard selectBorrowCardByUid(Integer uid) throws SQLException {
        String sql = "select b.bcid,b.password,b.reader,b.rule_id,b.status from borrow_card b inner join user u on u.uname =b.reader where u.uid=?";
        Object[] objects = {uid};
        BorrowCard borrowCard = DBUtils.DB.qr.query(sql, new BeanHandler<BorrowCard>(BorrowCard.class), objects);
        return borrowCard;
    }
    @Override
    public Rules selectRulesById(Integer rid) throws SQLException {
        String sql = "select id,borrow_num,limit_day,borrow_library,overTime_fee from rules where id=?";
        Object[] objects = {rid};
        Rules rules = DBUtils.DB.qr.query(sql, new BeanHandler<Rules>(Rules.class), objects);
        return rules;
    }

    @Override
    public Books selectBooksById(Integer bid) throws SQLException {
        String sql = "select bid,name,author,library_id,sort_id,position,state,description from `books` where bid=?";
        Object[] objects = {bid};
        Books books = DBUtils.DB.qr.query(sql, new BeanHandler<Books>(Books.class), objects);
        return books;
    }

    @Override
    public int addBooksBorrow(BooksBorrow booksBorrow) throws SQLException {
        String sql = "insert into borrow_books (card_id,book_id,borrow_date,end_date,user_id) values(?,?,?,?,?)";
        Object[] objects = {booksBorrow.getCard_id(),booksBorrow.getBook_id(),booksBorrow.getBorrow_date(),booksBorrow.getEnd_date(),booksBorrow.getUser_id()};
        int num = DBUtils.DB.qr.update(sql, objects);
        return num;
    }

    @Override
    public int updateBooksStatus(Integer bid) throws SQLException {
        String sql = "update books set state=0 where bid=?";
        Object[] objects = {bid};
        int num = DBUtils.DB.qr.update(sql, objects);
        return num;
    }
}
